package com.zut.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zut.base.common.entry.ResultCode;
import com.zut.base.common.entry.ResultVO;
import com.zut.base.common.util.TokenUtils;
import com.zut.base.entity.Manager;
import com.zut.base.entity.User;
import com.zut.base.service.IManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyl
 * @since 2021-04-07
 */
@RestController
@RequestMapping("/imp/manager")
public class ManagerController {
    @Autowired
    private IManagerService managerServiceImpl;

    @PostMapping("/login")
    public Object loginByPass(@RequestBody Manager manager) {
        if(manager==null) return new ResultVO<>(ResultCode.VALIDATE_FAILED, "请参数校验失败");
        if(manager.getAccount()==null || manager.getAccount().equals("")) return new ResultVO<>(ResultCode.VALIDATE_FAILED, "请参数校验失败");
        if(manager.getPassword()==null) return new ResultVO<>(ResultCode.VALIDATE_FAILED, "请求参数校验失败");
        QueryWrapper<Manager> managerQueryWrapper = new QueryWrapper<>();
        managerQueryWrapper.eq("account", manager.getAccount());
        managerQueryWrapper.and(i->i.eq("password", manager.getPassword()));
        Manager resultManager = managerServiceImpl.getOne(managerQueryWrapper);
        if(resultManager==null) {
            return new ResultVO<>(101, "用户名密码有误");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("status", "OK");
        map.put("token", "*");
        map.put("id", resultManager.getId());
        map.put("role", resultManager.getRole());
        return new ResultVO<>(200, "登陆成功", map);
    }

    @GetMapping("/findById")
    public Object findManagerById(@NotNull String mid) {
        if(mid==null || "".equals(mid)) return new ResultVO<>(ResultCode.VALIDATE_FAILED, "请参数校验失败");
        Manager manager = managerServiceImpl.getById(mid);
        if(manager==null) return new ResultVO<>(ResultCode.FAILED, "查找失败");
        Map<String, Object> map = new HashMap<>();
        map.put("body", manager);
        return new ResultVO<>(200, "查找成功", map);
    }
}
